<template>
	<view class="content withdrawAddress">
		<view class="add">
			<view class="w_input one">
				<view class="w_title">地址</view>
				<view class="w_block">
					<input type="text" v-model="address" placeholder-class="pla" placeholder="输入或长按粘贴地址" />
					<image @click="scan" src="../../../../static/sm.png" mode=""></image>
				</view>
			</view>
			<view class="w_input one">
				<view class="w_title">备注</view>
				<view class="w_block"><input type="text" v-model="name" placeholder-class="pla" placeholder="请输入备注" /></view>
			</view>
			<view class="blue_btn" @click="isDraw=true">
				确定
			</view>
		</view>
		<!-- 输入资金密码 -->
		<Modal :title="'添加地址'" v-if="isDraw" @close="closeModal('isDraw')">
			<view class="common_model">
				<view class="center_model">
					<view class="l_input">
						<text>短信验证</text>
						<input type="password" placeholder-class="pla" v-model="form.code" placeholder="请输入验证码" />
						<view v-if="numTime == false" class="send" @click="getCode()"><button plain>发送验证码</button></view>
						<view v-else class="send">
							<button plain disabled>{{ numText }}</button>
						</view>
					</view>
				</view>
				<view class="common_btn">
					<view @click="closeModal('isDraw')">取消</view>
					<view class="active" @click="addAddress()">确定</view>
				</view>
			</view>
		</Modal>
		<HMmessages ref="HMmessages" @complete="HMmessages = $refs.HMmessages"></HMmessages>
	</view>
</template>

<script>
import { uniIcon } from '@dcloudio/uni-ui';
import service from '../../service.js';
import HMmessages from '@/components/HM-messages/HM-messages.vue';
import Modal from '@/components/lkex-modal/index.vue';
export default {
	components: {
		uniIcon,
		HMmessages,
		Modal
	},
	data() {
		return {
			isAddress: false,
			list: [],
			coin: 'ALL',
			name: '',
			address: '',
			isDraw: false,
		};
	},
	onLoad(option) {
		if (option && option.type) {
			this.coin = option.type;
		}
	},
	onShow() {
		this.getList();
	},
	methods: {
		//扫描二维码
		scan() {
			let that = this;
			uni.scanCode({
				success: function(res) {
					// console.log('条码类型：' + res.scanType);
					that.address = res.result;
					// console.log('条码内容：' + res.result);
				}
			});
		},
		//隐藏弹窗
		closeModal(name) {
			this[name] = !this[name];
		},
		//选择地址
		selectAddress(item) {
			this.$store.commit('setAddress', item.address);
			uni.navigateBack({
				delta: 1
			});
		},
		//添加地址
		addAddress() {
			if (this.list.length >= 5) {
				this.errors('地址最多添加五个，请删除部分地址重试');
				return false;
			}
			if (!this.name) {
				this.errors('请输入地址名称');
				return false;
			}
			if (!this.address) {
				this.errors('请输入地址');
				return false;
			}
			this.$api.showLoading();
			service
				.addAddress({
					address: this.address,
					assetCode: this.coin,
					memo: this.name
				})
				.then(({ data }) => {
					this.$api.hideLoading();
					if (data.code == '100200') {
						this.closeModal('isAddress');
						this.getList();
						this.HMmessages.show('添加地址成功', {
							textAlign: 'center',
							background: '#356e36',
							fontColor: '#fff'
						});
					} else {
						this.errors(data.msg);
					}
				});
		},
		//获取地址列表
		getList() {
			service
				.addressList({
					assetCode: this.coin,
					pageNo: 1,
					pageSize: 5
				})
				.then(({ data }) => {
					if (data.code == '100200') {
						this.list = data.data.list;
					} else {
						this.errors(data.msg);
					}
				});
		},
		//删除地址
		del(item) {
			let that = this;
			uni.showModal({
				title: '删除地址',
				content: '删除地址后无法恢复，是否操作？',
				success(res) {
					if (res.confirm) {
						that.$api.showLoading();
						service
							.delAddress({
								id: item.addressid
							})
							.then(({ data }) => {
								that.$api.hideLoading();
								if (data.code == '100200') {
									that.getList();
									that.HMmessages.show('删除地址成功', {
										textAlign: 'center',
										background: '#356e36',
										fontColor: '#fff'
									});
								} else {
									that.errors(data.msg);
								}
							});
					}
				}
			});
		},
		//错误提示
		errors(text) {
			this.HMmessages.show(text, {
				textAlign: 'center',
				background: '#74272d',
				fontColor: '#fff'
			});
		},
		//隐藏弹窗
		closeModal(name) {
			this[name] = !this[name];
			this.name = '';
			this.address = '';
		}
	}
};
</script>

<style lang="scss">
@import './style.scss';
</style>
